#include <GL/glfw.h>
#include "easyvid.h"
#include "GLTexture.h"


VidSource* g_vsrc = NULL;
GLTexture* g_tex = NULL;
int g_counter = 0;

struct PLAYER
{
    VidSource* video;
    GLTexture* image;
    int ready;
};

PLAYER* playerOpen(const char* filename)
{
    PLAYER* pl = new PLAYER();
    pl->video = vidOpen(filename);
    pl->image = NULL;
    pl->ready = 0;
    return pl;
}

void playerClose(PLAYER* pl)
{
    vidClose(pl->video);
    delete pl->image;
    delete pl;
}

void playerUpdate(PLAYER* pl, double dt)
{
    pl->ready = vidUpdate(pl->video, dt);
    if (pl->ready)
    {
        VidFrame frm;
        vidGetFrame(pl->video, &frm);
        if (pl->image == NULL)
        {
            pl->image = new GLTexture(frm.width, frm.height);
        }
        pl->image->update(frm.data);
    }
}

void glDrawImage(GLTexture* tex, float x, float y, float w, float h)
{
    if (!tex)
        return;
    tex->drawImage(x, y, x+w, y+h);
}

void display(PLAYER** pls, int pls_count)
{
    int ww, hh;
    glfwGetWindowSize(&ww, &hh);
    glViewport(0, 0, ww, hh);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, ww, 0, hh);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(1, 1, 1);
    float x = 0;
    float y = 0;
    float w = 200;
    float h = 200;
    for (int i = 0; i < pls_count; i++)
    {
        glDrawImage(pls[i]->image, x, y, w, h);
        x += w;
        if (x > ww - w)
        {
            x = 0;
            y += h;
        }
    }
}

int main(int argc, char* argv[])
{
    glfwInit();
    glfwOpenWindow(800, 600, 8, 8, 8, 8, 8, 0, GLFW_WINDOW);
    glfwSwapInterval(0);

    vidInit();

    double t1 = glfwGetTime();
    double dt = 0.0;
    int i;

    #ifdef __WIN32__
    const int pls_count = 9;
    PLAYER* pls[pls_count];
    pls[0] = playerOpen("c:\\videostreams\\Stargate SG-1 - 004 - Emancipation.avi");
    pls[1] = playerOpen("c:\\videostreams\\Stargate SG-1 - 003 - The Enemy Within.avi");
    pls[2] = playerOpen("c:\\videostreams\\Elephants_Dream_1024.avi");
    pls[3] = playerOpen("c:\\videostreams\\big_buck_bunny_720p_stereo.avi");
    pls[4] = playerOpen("c:\\videostreams\\test1.avi");
    pls[5] = playerOpen("c:\\videostreams\\test3.avi");
    pls[6] = playerOpen("c:\\videostreams\\test2.wmv");
    pls[7] = playerOpen("c:\\videostreams\\SO.2009.WS.QF_Wang.Yihan_-_Zhou.Mi.wmv");
    pls[8] = playerOpen("c:\\videostreams\\SO.2009.MS.QF_Lee.Chong.Wei_-_Taufik.Hidayat.wmv");
    #else
    const int pls_count = 1;
    PLAYER* pls[pls_count];
    pls[0] = playerOpen("/scratch/test3.avi");
    /*pls[1] = playerOpen("/scratch/test2.wmv");
    pls[2] = playerOpen("/scratch/test1.avi");
    pls[3] = playerOpen("/scratch/ed.avi");*/
    #endif

    while (glfwGetWindowParam(GLFW_OPENED))
    {
        //input
        glfwPollEvents();
        if (glfwGetKey(GLFW_KEY_ESC))
            break;
        //update
        dt = glfwGetTime() - t1;
        t1 = glfwGetTime();
        for (i = 0; i < pls_count; i++)
            playerUpdate(pls[i], dt);
        //draw
        glClear(GL_COLOR_BUFFER_BIT);
        display(pls, pls_count);
        glfwSwapBuffers();
    }

    for (i = 0; i < pls_count; i++)
        playerClose(pls[i]);

    glfwTerminate();

    return 0;
}

